package com.htjx.mall.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.htjx.mall.commons.mappers.CatalogMapper;
import com.htjx.mall.commons.models.enums.SeqType;
import com.htjx.mall.commons.models.product.Catalog;

@Service
public class CatalogService {
	@Autowired
	private CatalogMapper catalogMapper;
	@Autowired
	private SequenceManager sequenceManager;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	public void insertOne(final Catalog catalog) {
		long[] seq = sequenceManager.buildBatchSeq(SeqType.Catalog, 1);
		catalog.setId(seq[0]);
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				catalogMapper.insertOne(catalog);
			}
		});
	}
	
	public Catalog selectById(long id) {
		return catalogMapper.selectOne(id);
	}
	
	public void updateOne(final Catalog catalog) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				catalogMapper.updateOne(catalog);
			}
		});
	}
	
	public void deleteOne(final long id) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				catalogMapper.deleteOne(id);
			}
		});
	}
	
	public List<Catalog> SelectByProductId(long productId) {
		return catalogMapper.selectByProductId(productId);
	}
	
}
